package class21To30;

import java.io.*;

public class RandomSelect {

    public static int first = 0;

    public static int last = 0;

    public static int n;

    public static int k;

    public static int findKthLargest(int[] nums, int k) {
        return randomSelect(nums, nums.length - k);
    }

    private static int randomSelect(int[] arr, int k) {
        int ans = 0;
        for (int l = 0, r = arr.length - 1; l <= r; ) {
            partition(arr, l, r, arr[l + (int) (Math.random() * (r - l + 1))]);
            if (k < first) {
                r = first - 1;
            } else if (k > last) {
                l = last + 1;
            } else {
                ans = arr[k];
                break;
            }
        }
        return ans;
    }

    public static void partition(int[] arr, int l, int r, int x) {
        first = l;
        last = r;
        int i = l;
        while (i <= last) {
            if (arr[i] == x) {
                i++;
            } else if (arr[i] < x) {
                swap(arr, first++, i++);
            } else {
                swap(arr, i, last--);
            }
        }
    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        in.nextToken();
        n = (int) in.nval;
        in.nextToken();
        k = (int) in.nval;
        int arr[] = new int[n];
        for (int i = 0; i < n; i++) {
            in.nextToken();
            arr[i] = (int) in.nval;
        }
        System.out.println(findKthLargest(arr, n - k));
        out.flush();
        out.close();
        br.close();
    }
}
